<?php

namespace app\common\services;

use app\common\model\CourseQuestion as CourseQuestionModel;
use app\common\model\CourseQuestionCategory as CourseQuestionCategoryModel;
use think\facade\Db;

use think\facade\Cache;

class CourseQuestionService
{
    
    // 课程下试题
    public static function info($id){
        
        $info = CourseQuestionModel::where('id',$id)->find();
        return $info;
    }
    
    /**
     * 添加
     */
    public static function add($data){
        
        $feedback = new CourseQuestionModel;
        $feedback->allowField(true)->save($data);
    }


    public static function edit(array $data)
    {
        return CourseQuestionModel::where('id',$data['id'])->update($data);
    }

    public static function del($id)
    {
        return CourseQuestionModel::where('id',$id)->delete();
    }

    public static function list(array $data)
    {   
        if(!empty($data['cid'])){
            $ids = CourseQuestionCategoryModel::where('content_id',(9000+$data['cid']))->column('id');
            $where['c.cate_id'] = ['IN',$ids];
        }else{
            $where['c.cate_id'] = ['eq',$data['c.cate_id']];
        }
        $min = ($data['page'] - 1) * $data['num'];
        $num = $data['num'];
        unset($data);
        $result = CourseQuestionModel::alias("c")
            ->join('exam_question q',"c.question_id=q.id",'right')
            ->join('yalimedia_course_question_category cate','c.cate_id = cate.id','left')
            ->join('yalimedia_question_log_detail detail',"detail.question_id=q.id AND detail.course_id = c.cate_id",'left')
            ->where($where)
            ->limit($min, $num)
            ->order('c.id desc')
            ->field('q.*,q.xf_content as q_content,cate.name as cate_name,detail.content')
            ->select();
        foreach($result as $key => &$value){

            if (isset($value['options_json']) && $value['options_json'] && !is_numeric($value['options_json'])) {
                $value['options_json'] = \json_decode($value['options_json'],true);
            }

            $xf_text = '';
            if($value['kind'] == 'WORD'){
                // 单词、句子
                if(!empty($value['q_content'])){
                    
                    if($value['is_word'] == 0){
                        $xf_text .= '[word]';
                    }else{
                        $xf_text .= '[content]';
                    }
                    $xf_text .= $value['q_content'];

                    if(!empty($value['vocabulary'])){
                        $xf_text .= '[vocabulary]'.$value['vocabulary'];
                    }

                    if(!empty($value['number_replace'])){
                        $xf_text .= '[number_replace]'.$value['number_replace'];
                    }
                }
                $value['xf_text'] = $xf_text;
            };

            if($value['kind'] == 'CALL'){
             
                // 模拟通话
                if(!empty($value['answer'])){
                    
                    $answer = json_decode($value['answer'],true);
                    
                    $xf_answer = [];
                    foreach ($answer['config'] as $k => $v) {

                        $xf_th_text = '';
                        if(!empty($v['content'])){
                       
                            $xf_th_text .= '[content]'.$v['content'];
        
                            if(!empty($v['vocabulary'])){
                                $xf_th_text .= '[vocabulary]'.$v['vocabulary'];
                            }
        
                            if(!empty($v['number_replace'])){
                                $xf_th_text .= '[number_replace]'.$v['number_replace'];
                            }
                        }

                        $xf_answer['answer'][$k]['id']        = $v['id'];
                        $xf_answer['answer'][$k]['name']      = $v['name'];
                        $xf_answer['answer'][$k]['answer']    = $v['answer'];
                        $xf_answer['answer'][$k]['start_at']  = $v['start_at'];
                        $xf_answer['answer'][$k]['xf_text']   = $xf_th_text;

                        $xf_answer['user']   = $answer['user'];
                    }
                    $value['answer'] = $xf_answer;
                }
            }
        }
        return $result;
    }

    public static function readList($id,$page_info){

        $list = CourseQuestionCategoryModel::where(['content_id'=>$id,'pid'=>0])->field('id,name')->paginate($page_info['num'])->toArray();
        if(empty($list)){
            return [];
        }

        $ids  = array_column($list['data'],'id');
        
        $questionList = CourseQuestionModel::alias("c")
                        ->where('c.cate_id','IN',$ids)
                        ->join('exam_question q',"c.question_id=q.id",'left')
                        ->field('q.id,c.cate_id as cate_id,q.title,q.difficulty,q.m_type,q.short_title')
                        ->select();
        if(empty($questionList)){
            return $list;
        }


        // 所有ID
        $all_ids = CourseQuestionCategoryModel::where(['content_id'=>$id,'pid'=>0])->column('id');
        $question_total = CourseQuestionModel::where('cate_id','IN',$all_ids)->count();

        $res = array();
        foreach ($questionList as $value) {
            $res[$value['cate_id']][] = $value;
        }

        foreach($list['data'] as &$value){
            $value['sub'] = $res[$value['id']];

            unset($value);
        }

        $list['question_total'] = $question_total;
        return $list;
    }
}